import * as NS from "../../Common/NetServices";
import { IsNull, IsNullOrWhiteSpace } from "../../TYLibrary/Common/GlobalExtend";
import { Wx_XSPPage, T_ReceivingAddress } from "../../Common/Entitys";
import { Log } from "../../TYLibrary/System/Logger";
import { Wx_PageData, WXDataClassRuntimeDecorator } from "../../Common/EntityBase";

@WXDataClassRuntimeDecorator()
class AddressPageData extends Wx_PageData {
    public region = [];
    public ISDef: boolean = false;
    public flag: boolean = false;
    public btnflag: any = false;
    public addlabflag: boolean = false;
    public Itext: string = "";
    public labelItem = [
        { name: "jiao", value: "家", checked: "" },
        { name: "gc", value: "公司", checked: "" },
        { name: "xxiao", value: "学校", checked: "" },
        { name: "md", value: "门店", checked: "" },
        { name: "vip", value: "Vip", checked: "" }
    ];
    public labactive: number = 0;
    public USERNAME = null;
    public TELNUMBER = null;
    public IDCARDNO = null;
    public DETAILINFO = null;
    public PROVINCENAME: string = "";
    public CITYNAME: string = "";
    public COUNTYNAME: string = "";
}

class Address extends Wx_XSPPage {
    private m_OldData: T_ReceivingAddress = null;
    private m_Type: string;
    private m_Adderss: number;
    public Data: AddressPageData = new AddressPageData();

    /**
     * 初始化页面信息
     * @param {*} options
     * @returns {Promise<void>}
     * @memberof Address
     */
    public async InitData(options: any): Promise<void> {
        try {
            this.Data.flag = false;
            this.Data.btnflag = false;
            this.Data.addlabflag = false;
            this.Data.labactive = 0;
            this.Data.labelItem = this.Data.labelItem;
            await this.Data.ApplyChange(this);
            this.m_Type = options.Type;
            this.m_Adderss = options.ID;
            console.log(this.m_OldData);
            console.log(" this.m_Type", this.m_Type, options.ID);
            console.log(this.App.ReceivingAddress, "this.App.ReceivingAddress");
            if (this.m_Type == "Edit") {
                this.m_OldData = this.App.ReceivingAddress.Default(
                    item => item.ID == this.m_Adderss
                );
                console.log(this.m_OldData);
                if (IsNull(this.m_OldData)) {
                    wx.navigateTo({
                        url: "Index"
                    });
                    this.ShowError("数据没有找到！");
                } else {
                    console.log("this.m_OldData", this.m_OldData);

                    let c = this.m_OldData;
                    let arr = [];
                    for (let s = 0; s < this.Data.labelItem.length; s++) {
                        if (this.Data.labelItem[s].name == c.LABEL) {
                            this.Data.labelItem[s].checked = "labactive";
                        } else {
                            this.Data.labelItem[s].checked = "";
                        }
                        arr.push(this.Data.labelItem[s]);
                    }
                    this.Data.USERNAME = c.USERNAME;
                    this.Data.TELNUMBER = c.TELNUMBER;
                    this.Data.IDCARDNO = c.IDCARDNO;
                    this.Data.DETAILINFO = c.DETAILINFO;
                    this.Data.PROVINCENAME = c.PROVINCENAME;
                    this.Data.CITYNAME = c.CITYNAME;
                    this.Data.COUNTYNAME = c.COUNTYNAME;
                    this.Data.ISDef = c.ISDEFAULT;
                    this.Data.labelItem = arr;
                    await this.Data.ApplyChange(this);
                }
            }
        } catch (e) {}
        this.Data.flag = false;
    }

    /**
     * 收货地址改变时
     * @param {*} e
     * @memberof Address
     */
    public RegionChangeHandler(e: any): void {
        this.Data.PROVINCENAME = e.detail.value[0];
        this.Data.CITYNAME = e.detail.value[1];
        this.Data.COUNTYNAME = e.detail.value[2];

        this.Data.region = e.detail.value;
        this.Data.PROVINCENAME = this.Data.PROVINCENAME;
        this.Data.CITYNAME = this.Data.CITYNAME;
        this.Data.COUNTYNAME = this.Data.COUNTYNAME;
        this.Data.ApplyChange(this);
    }

    /**
     * 修改标签时
     * @param {*} e
     * @memberof Address
     */
    public InputHandler(e: any): void {
        let s = e.detail.value;
        this.Data.Itext = s;
        console.log(s, "e.detail.value");
        if (!IsNull(s) && s != "") {
            this.Data.btnflag = true;
        } else {
            this.Data.btnflag = false;
        }
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @memberof Address
     */
    public EditHandler(e: any): void {
        if (this.Data.addlabflag == true) {
            this.Data.addlabflag = false;
            this.Data.btnflag = true;
            this.Data.ApplyChange(this);
        }
    }

    /**
     *
     * @param {*} e
     * @memberof Address
     */
    public FinishHandler(e: any): void {
        if (this.Data.addlabflag == false) {
            this.Data.addlabflag = true;
            this.Data.btnflag = "finishInput";
            this.Data.ApplyChange(this);
        }
    }

    /**
     *
     * @param {*} e
     * @memberof Address
     */
    public ShowInputHandler(e: any): void {
        if (this.Data.flag == false) {
            this.Data.flag = true;
            this.Data.ApplyChange(this);
        }
    }

    /**
     *
     * @param {*} e
     * @memberof Address
     */
    public CheckedHandler(e: any): void {
        let names = e.currentTarget.dataset.name;
        let arr = [];
        for (let s = 0; s < this.Data.labelItem.length; s++) {
            if (this.Data.labelItem[s].name == names) {
                this.Data.labelItem[s].checked = "labactive";
            } else {
                this.Data.labelItem[s].checked = "";
            }
            arr.push(this.Data.labelItem[s]);
        }
        this.Data.labelItem = arr;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @memberof Address
     */
    public Switch1ChangeHandler(e: any): void {
        this.Data.ISDef = e.detail.value;
    }

    /**
     *
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof Address
     */
    public async AddressHandler(e): Promise<void> {
        let userName = e.detail.value.USERNAME;
        let telnumber = e.detail.value.TELNUMBER;
        let IdCardNo = e.detail.value.IDCARDNO;

        let province = this.Data.PROVINCENAME;
        let city = this.Data.CITYNAME;
        let county = this.Data.COUNTYNAME;
        let detailInfo = e.detail.value.DRTAILINFO;
        let label = "";
        let ISDefault = this.Data.ISDef;
        for (let s = 0; s < this.Data.labelItem.length; s++) {
            if ((this.Data.labelItem[s].checked = "labactive")) {
                label = this.Data.labelItem[s].name;
            }
        }
        console.log(
            userName,
            telnumber,
            IdCardNo,
            province,
            city,
            county,
            detailInfo,
            label,
            ISDefault
        );
        if (IsNullOrWhiteSpace(userName)) {
            wx.showModal({
                title: "提示",
                content: "收货人名称不可为空！",
                showCancel: false
            });
            return;
        }
        if (IsNullOrWhiteSpace(telnumber)) {
            wx.showModal({
                title: "提示",
                content: "收货人联系手机号不可为空！",
                showCancel: false
            });
            return;
        }
        if (telnumber.length < 11) {
            wx.showModal({
                title: "提示",
                content: "请输入正确的收货人联系手机号！",
                showCancel: false
            });
            return;
        }
        if (IsNullOrWhiteSpace(detailInfo)) {
            wx.showModal({
                title: "提示",
                content: "收货人详细地址不可为空！"
            });
            return;
        }
        if (IsNullOrWhiteSpace(province) || IsNullOrWhiteSpace(county)) {
            wx.showModal({
                title: "提示",
                content: "收货人省市区不可为空！",
                showCancel: false
            });
            return;
        }
        try {
            let d = new T_ReceivingAddress();
            d.MEMBERID = wx.getStorageSync("MEMBERID");
            d.PLATFORMID = wx.getStorageSync("PLATFORMID");
            d.USERNAME = userName;
            d.TELNUMBER = telnumber;
            d.IDCARDNO = IdCardNo;
            d.PROVINCENAME = province;
            d.CITYNAME = city;
            d.COUNTYNAME = county;
            d.DETAILINFO = detailInfo;
            d.LABEL = label;
            d.ISDEFAULT = ISDefault;
            if (this.m_Type == "Edit") {
                d.ID = this.m_Adderss;
                await NS.EditUserAddress(d);
            } else {
                await NS.AddMemberAddress(d);
            }
            wx.navigateBack({ delta: 1 });
        } catch (e) {
            this.ShowError(e);
        }
    }
}

Page(new Address());
